import 'dart:math';

import 'package:color_dart/color_dart.dart';
import 'package:flutter/material.dart';
import 'package:local_coffee/components/c_swiper/custom_swiper.dart';
import 'package:local_coffee/components/goods_detail/goods_detail_dialog.dart';
import 'package:local_coffee/components/rgba.dart';
import 'package:local_coffee/jsonserialize/goods_category/data.dart';
import 'package:local_coffee/jsonserialize/goods_list/data.dart';
import 'package:local_coffee/pages/toolbar/menu/category.dart';
import 'package:local_coffee/pages/toolbar/menu/goods_list_row.dart';
import 'package:local_coffee/pages/toolbar/menu/testData.dart';
import 'package:local_coffee/pages/toolbar/menu/widgets/classify_desc.dart';
import 'package:local_coffee/provider/shopping_cart_model.dart';
import 'package:local_coffee/routes/index.dart';
import 'package:local_coffee/utils/global.dart';
import 'package:provider/provider.dart';

/// TODO: 锚点
/// 点击左侧菜单, 右侧商品列表跳转
class Menu extends StatefulWidget {
  
  static _MenuState _menuState;

  getAppBar() => _menuState.createAppBar();

  Menu() {
    _menuState = _MenuState();
  }

  _MenuState createState() => _MenuState();
}

class _MenuState extends State<Menu> {
  
  /// 当前选中的菜单
  static int nowCategoryId;
  static double _nestedScrollOffet = 0;

  final ScrollController _nestedScrollController = new ScrollController();

  Map<String, dynamic> categoryCobineGoods = {};

  final List actives = ['满50减40', '充2赠2', '买2送3'];

  /// 把商品列表，和分类的部分设置为变量。
  /// 解决：滚动页面，重复渲染
  List<Widget> goodsListWidgets = [];
  List<GoodsCategoryData> category = [];

  AppBar createAppBar() {
    return null;
  } 

  @override
  void initState() {
    _nestedScrollController.addListener(() {
      setState(() {
        _nestedScrollOffet = _nestedScrollController.offset;
      });
    });

    Future.delayed(Duration.zero, () {
      _init(context);
    });

    super.initState();
  }

  _init(BuildContext context) async {
    G.loading.show(context);

    try {
      Iterable<Future> requestList = [
        G.req.shop.goodsList(),
        G.req.shop.goodsCategoryAll()
      ];

      List result = await Future.wait(requestList);

      GoodsList goodsList = GoodsList.fromJson(result[0].data);
      int goodsListLen = goodsList.data.length;
      GoodsCategory goodsCategory = GoodsCategory.fromJson(result[1].data);

      List<Widget> goodsListWidgetsTemp = [];
      Random rand = Random();

      goodsCategory.data.forEach((GoodsCategoryData category) {
        // 商品列表 每类商品 标题 eg: 人气 top
        goodsListWidgetsTemp.add(ClassifyDesc(
          category.name,
          desc: null,
        ));

        goodsList.data.asMap().forEach((int index, GoodsListDatum goods) {
          if (category.id == goods.categoryId) {
            // 商品列表 商品
            goodsListWidgetsTemp.add(GoodsListRow(
              // 点击添加按钮 弹出 dialog
              onPress: (BuildContext context, int id) {
                // 弹出商品详情 /widgets/goods_detail
                showDialog(
                  context: context,
                  barrierDismissible: false,
                  builder: (BuildContext context) {
                    final ShoppingCartModel _shoppingCartModer = 
                      Provider.of<ShoppingCartModel>(context);
                    return GoodsDetailDialog(
                      id: id,
                      model: _shoppingCartModer,
                    );
                  }
                );
              },
              data: goods,
              border: !(index >= goodsListLen - 1),
              activeDesc: actives[rand.nextInt(3)],
            ));
          }
        });
      });

      setState(() {
        nowCategoryId = goodsCategory.data[0].id;
        goodsListWidgets = goodsListWidgetsTemp;
        category = goodsCategory.data;
      });

      G.loading.hide(context);
    } catch (e) {
      print(e);
      G.loading.hide(context);
    }
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: NestedScrollView(
        controller: _nestedScrollController,
        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
          return <Widget>[
            SliverAppBar(
              expandedHeight: 186,
              pinned: true,
              floating: false,
              elevation: 0,
              bottom: PreferredSize(
                child: Container(
                  decoration: BoxDecoration(border: G.borderBottom()),
                ),
                preferredSize: Size.fromHeight(0),
              ),
              title: Text(
                '选择咖啡和小食',
                style: TextStyle(
                  color: RGBA.rgba383838,
                  fontSize: 16,
                  fontWeight: FontWeight.bold
                ),
              ),
              backgroundColor: Colors.white,
              flexibleSpace: FlexibleSpaceBar(
                background: Container(
                  margin: EdgeInsets.only(top: 56),
                  child: InkWell(
                    child: Opacity(
                      opacity: 1,
                      child: CustomSwiper([
                        P.swiper1,
                        P.swiper2
                      ], height: 130,),
                    ),
                    onTap: () {},
                  ),
                ),
              ),
            )
          ];
        },
        body: Container(
          padding: EdgeInsets.only(
            top: _nestedScrollOffet >= 130 ? (_nestedScrollOffet - 130) : 0
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              // 左侧菜单列表
              Container(
                width: 90,
                color: RGBA.rgbaf8f8f8,
                child: Category(
                  data: category,
                  id: nowCategoryId,
                  getCategoryId: (id) {
                    if (nowCategoryId != id) {
                      setState(() {
                        nowCategoryId = id;
                      });
                    }
                  },
                ),
              ),
              // 右侧商品
              Container(
                width: G.screenWidth() - 90,
                padding: EdgeInsets.symmetric(horizontal: 14),
                // 使用listview中的scrollcontronal导致外层的NestedScrollView效果失效，
                // 使用NotificationListener完美解决
                child: NotificationListener(
                  child: ListView(
                    physics: _nestedScrollOffet >= 130
                      ? BouncingScrollPhysics()
                      : ClampingScrollPhysics(),
                    children: goodsListWidgets,
                  ),
                  onNotification: (ScrollNotification scrollInfo) {
                    return null;
                  },
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}